package com.gitee.feizns.itrip.personal.biz.service.impl;

import com.gitee.feizns.itrip.personal.beans.pojo.Comment;
import com.gitee.feizns.itrip.personal.beans.pojo.HotelOrder;
import com.gitee.feizns.itrip.personal.beans.pojo.Image;
import com.gitee.feizns.itrip.personal.beans.vo.Page;
import com.gitee.feizns.itrip.personal.beans.vo.impl.PageVoForMybatisPlus;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptCommentAdd;
import com.gitee.feizns.itrip.personal.biz.entity.ao.ForAcceptGetCommentList;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetCommentList;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetCount;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewGetImg;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewHotelLable;
import com.gitee.feizns.itrip.personal.biz.entity.vo.ForViewHotelScore;
import com.gitee.feizns.itrip.personal.biz.mapper.BizCommentMapper;
import com.gitee.feizns.itrip.personal.biz.mapper.BizHotelOrderMapper;
import com.gitee.feizns.itrip.personal.biz.mapper.BizImageMapper;
import com.gitee.feizns.itrip.personal.biz.mapper.BizLabelDicMapper;
import com.gitee.feizns.itrip.personal.biz.service.CommentService;
import com.gitee.feizns.itrip.personal.biz.service.exception.CommentException;
import com.gitee.feizns.itrip.personal.mapper.CommentMapper;
import com.gitee.feizns.itrip.personal.mapper.HotelOrderMapper;
import com.gitee.feizns.itrip.personal.mapper.ImageMapper;
import com.gitee.feizns.itrip.personal.mapper.LabelDicMapper;
import com.gitee.feizns.itrip.personal.utils.FTPUtils;
import com.gitee.feizns.itrip.personal.utils.Utils;
import lombok.NonNull;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import static com.gitee.feizns.itrip.personal.biz.service.exception.CommentException.COMMENT_ORDER_ID_IS_NOT_NULL;

/**
 * @author feizns
 * @since 2019/1/9 0009
 */
@Service
public class CommentServiceImpl implements CommentService {

    /**
     * 被忽略的isOk值
     */
    private static final int IGNORE_IS_OK_VALUE = -1;

    /**
     * 被忽略的isHavingImg值
     */
    private static final int IGNORE_IS_HAVING_IMG_VALUE = -1;

    /**
     * 默认的分数小数点位数
     */
    private static final int DEFAULT_SCORE_DECIMAL_POINT = 1;

    /**
     * 评论放置目录
     */
    private static final String COMMENT_DIRECTORY = "comment";

    @Autowired
    private BizImageMapper bizImageMapper;

    @Autowired
    private BizCommentMapper bizCommentMapper;

    @Autowired
    private BizLabelDicMapper bizLabelDicMapper;

    @Autowired
    private BizHotelOrderMapper bizHotelOrderMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private ImageMapper imageMapper;

    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private FTPUtils ftpUtils;

    @Override
    public List<ForViewGetImg> getImg(Integer commentId) {
        List<ForViewGetImg> img = bizImageMapper.getImg(ImageMapper.IMG_COMMENT_TYPE, commentId);
        return img;
    }

    @Override
    public Page<ForViewGetCommentList> getCommentList(@NonNull ForAcceptGetCommentList condition) {

        //纠正条件
        correction(condition);

        com.baomidou.mybatisplus.plugins.Page<ForViewGetCommentList> page =
                new com.baomidou.mybatisplus.plugins.Page<>(condition.getPageNo(), condition.getPageSize());

        page.setRecords(bizCommentMapper.getCommentList(page, condition));

        return new PageVoForMybatisPlus<>(page);
    }

    @Override
    public ForViewGetCount getCount(Integer hotelId) {
        return bizCommentMapper.getCount(hotelId);
    }

    @Override
    public ForViewHotelScore getHotelScore(Integer hotelId) {
        return bizCommentMapper.getHotelScore(hotelId, DEFAULT_SCORE_DECIMAL_POINT);
    }

    @Override
    public List<ForViewHotelLable> getTravelType() {
        return bizLabelDicMapper.selectByParentId(LabelDicMapper.TRIP_PARENT_ID);
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public void add(ForAcceptCommentAdd comment, Long curtUserId) {
        Comment newComment = checkForAcceptAdd(comment, curtUserId);
        commentMapper.insert(newComment);

        //更新订单状态
        hotelOrderMapper.updateById(createHotelOrder(comment, curtUserId));

        ForAcceptCommentAdd.Image[] images = comment.getItripImages();
        if ( images != null ) {
            for (int i = 0; i < images.length; i++) {
                Image image = createImg(i + 1, ImageMapper.IMG_COMMENT_TYPE, images[i], newComment.getId(), curtUserId);
                imageMapper.insert(image);
            }
        }
    }

    @Override
    public String[] uploadImg(InputStream file, Long curtUserId, String suffix) {
        String imgUrl = CommentService.generateImageUrl(curtUserId, suffix);
        boolean file1 = ftpUtils.uploadFile(COMMENT_DIRECTORY, imgUrl, file);
        return new String[]{ COMMENT_IMAGE_PREFIX + imgUrl };
    }

    //创建Image
    private static Image createImg(Integer position, String type, ForAcceptCommentAdd.Image image, Long targetId, Long curtUserId) {
        Image img = new Image();
        img.setPosition(position);
        img.setType(type);
        img.setImgUrl(image.getImgUrl());
        img.setTargetId(targetId);
        img.setCreationDate(new Date());
        img.setCreatedBy(curtUserId);
        return img;
    }

    private HotelOrder createHotelOrder(ForAcceptCommentAdd newComment, Long curtUserId) {
        return new HotelOrder()
                .setId(newComment.getOrderId())
                .setOrderStatus(HotelOrderMapper.ORDER_REVIEWED_STATUS)
                .setModifiedBy(curtUserId)
                .setModifyDate(new Date());
    }

    //组装数据
    private Comment checkForAcceptAdd(ForAcceptCommentAdd comment, Long curtUserId) {
        if ( comment.getOrderId() == null )
            throw new CommentException(COMMENT_ORDER_ID_IS_NOT_NULL, "新增评论，订单ID不能为空");

        Comment newComment = new Comment();
        BeanUtils.copyProperties(comment, newComment);
        newComment.setCreationDate(new Date());
        newComment.setUserId(curtUserId);
        newComment.setScore(Utils.intSum(comment.getFacilitiesScore(), comment.getHygieneScore(), comment.getPositionScore(), comment.getServiceScore()));
        return newComment;
    }

    //纠正条件
    private static void correction(@NonNull ForAcceptGetCommentList condition) {
        if ( condition.getIsHavingImg() != null && condition.getIsHavingImg() == IGNORE_IS_HAVING_IMG_VALUE )
            condition.setIsHavingImg(null);

        if ( condition.getIsOk() != null && condition.getIsOk() == IGNORE_IS_OK_VALUE )
            condition.setIsOk(null);

        int curt = Page.MYBATIS_PLUS_DEFAULT_PAGE_NO;  //第一页

        int size = Page.DEFAULT_PAGE_SIZE;  //页大小

        if ( condition.getPageNo() != null && condition.getPageNo() > 0 )
            curt = condition.getPageNo();

        if ( condition.getPageSize() != null && condition.getPageSize() > 0 )
            size = condition.getPageSize();

        condition.setPageNo(curt);
        condition.setPageSize(size);
    }

}
